import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:honeypotflutter/public/public_alert.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:scan/scan.dart';

class HLScanf extends StatefulWidget {
  final double? width;
  final double? height;
  final double scanAreaScale;
  final CaptureCallback? onCapture;
  final ScanController? controller;
  final Color? lineColor;
  const HLScanf({
    super.key,
    this.width,
    this.height,
    this.scanAreaScale = .7,
    this.onCapture,
    this.controller,
    this.lineColor,
  });

  @override
  State<HLScanf> createState() => _HLScanfState();
}

class _HLScanfState extends State<HLScanf> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _requestPermissions();
  }

  /*
  * 请求权限
  * */
  _requestPermissions() async {
    await Permission.camera.onPermanentlyDeniedCallback(() {
      HLAlert.showSureCancelAlert(
          title: "权限提示",
          message: "您已拒绝APP访问使用您的相机，如需继续使用，请至设置中开启您的相机权限",
          cancelLabel: "取消",
          okLabel: "前往设置",
          sureAction: (bool ok) {
            if (ok) {
              openAppSettings();
            } else {
              Get.back();
            }
          });
    }).request();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: widget.height,
      width: widget.width,
      child: ScanView(
        controller: widget.controller,
        scanAreaScale: widget.scanAreaScale,
        scanLineColor: widget.lineColor ?? Colors.green.shade400,
        onCapture: widget.onCapture,
      ),
    );
  }
}
